//
//  AppContext.m
//  cleancar
//
//  Created by wangfang on 14-10-14.
//  Copyright (c) 2014年 edaijia-inc. All rights reserved.
//

#import "AppContext.h"
#import "ICAccountManager.h"
#import "ICNet.h"


@interface AppContext ()

@property (nonatomic, readwrite) ICAccountManager *accountManager;
@property (readwrite, nonatomic) ICNet *net;
@end

@implementation AppContext

+ (instancetype)instance {
    static AppContext *_instance = nil;
    
    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    
    return _instance;
}

- (void)dealloc {
    [self cleanupNotifications];
}

- (void)setUp{
    [self net];

    [self accountManager];
     [self.accountManager requestUserHost];
    [self.locationManager startLocation];
    [self shoppingCartManager];
    [self addressManager];
}

- (ICAccountManager *)accountManager {
    if (!_accountManager) {
        _accountManager = [[ICAccountManager alloc] init];
    }
    
    return _accountManager;
}

- (EFLocationManager *)locationManager{
    if (!_locationManager) {
        _locationManager = [[EFLocationManager alloc] init];
    }
    return _locationManager;
}

- (EFShoppingCartManager *)shoppingCartManager {
    if (!_shoppingCartManager) {
        _shoppingCartManager = [[EFShoppingCartManager alloc] init];
    }
    
    return _shoppingCartManager;
}

- (EFAddressManager *)addressManager{
    if (!_addressManager) {
        _addressManager = [[EFAddressManager alloc] init];
    }
    return _addressManager;
}

- (ICNet *)net{
    if (!_net) {
        _net = [[ICNet alloc] initWithBaseURL:[NSURL URLWithString:kBaseUrl]];
    }
    return _net;
}


- (NSString *)pathOfCacheFileAllKeyValueObj {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [paths objectAtIndex:0];
    return [libraryPath stringByAppendingPathComponent:@"allKeyValue.txt"];
}

- (NSString *)bdMapApiKey {
    //#ifdef API_ENV_TEST //百度地图的key企业包不同于线上包
    //    return @"I7yNcdXIXDCLgqfIVn4jhoc5";
    //#else
    //    return @"0TGnzlUnIeNusN2GEGRO5NGs";
    //#endif
    NSString *packageName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey];
    if ([packageName isEqualToString:@"ccom.eatown.appstore"]) {
        return @"heA1Zhpt3vQB3T2VRqCr7A6Dx9YHRW1j";
    } else if ([packageName isEqualToString:@"com.eatown.develop"]) {
        return @"o3ccZlc3TAqi0wlpkEsiByPW5ougYpqj";
    } else if([packageName isEqualToString:@"com.eatown.adhoc"]){
        return @"McMWlx0HMGWYP4sROvs1P98jHSDSVwby";
    }
    return @"heA1Zhpt3vQB3T2VRqCr7A6Dx9YHRW1j";
}

#pragma mark Notification
- (void)setupNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userLoginNotification:)
                                                 name:IC_ACCOUNT_DID_LOGIN_NOTIFICATION
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userLogoutNotification:)
                                                 name:IC_ACCOUNT_DID_LOGOUT_NOTIFICATION
                                               object:nil];
}

- (void)cleanupNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)userLoginNotification:(NSNotification *)notification
{
    
}

- (void)userLogoutNotification:(NSNotification *)notification
{

}



@end
